<template>
    <div class="reg-container">
      <el-form ref="regForm" :model="regForm" :rules="regRules" size="medium" label-width="100px"
      class="reg-form" auto-complete="on" label-position="left">
        <div class="title-container">
            <h3 class="title">注册</h3>
        </div>
        <el-form-item label="用户名" prop="account">
          <el-input v-model="regForm.account" placeholder="请输入用户名" clearable prefix-icon='el-icon-user-solid'
            :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="pwd">
          <el-input v-model="regForm.pwd" placeholder="请输入密码" clearable prefix-icon='el-icon-key'
          :type="passwordType" show-password :style="{width: '100%'}">
          <span class="show-pwd" @click="showPwd">
            <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
          </span>
        </el-input>
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input v-model="regForm.name" placeholder="请输入名称" clearable prefix-icon='el-icon-user'
            :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-radio-group v-model="regForm.sex" size="medium">
            <el-radio label="男"></el-radio>
            <el-radio label="女"></el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="年龄" prop="age">
      <el-input v-model.number="regForm.age" placeholder="请输入年龄" autocomplete="off" prefix-icon="el-icon-s-custom" clearable></el-input>
      </el-form-item>
        <el-form-item label="电话" prop="tel">
          <el-input v-model="regForm.tel" placeholder="请输入电话" clearable prefix-icon='el-icon-phone'
            :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="电子邮箱" prop="email">
          <el-input v-model="regForm.email" placeholder="请输入电子邮箱" clearable prefix-icon='el-icon-message'
            :style="{width: '100%'}"></el-input>
        </el-form-item>
    
        <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click.native.prevent="handleReg">注册</el-button>

        <div class="tips">
        <span style="margin-right:20px;">
          <el-button type="text" @click.native.prevent="toLog">已有账号？去登录</el-button>
        </span>
      </div>
      </el-form>
    </div>
  </template>
  
  <script>
  import { register } from '@/api/user'
  
  export default {
    name: 'Register',
    data() {
      const validatePassword = (rule, value, callback) => {
        if (value.length < 6) {
          callback(new Error('密码不能少于6位'))
        } else {
          callback()
        }
      }
      return {
        regForm: {
          account: '',
          pwd: '',
          name: '',
          sex: '',
          tel: '',
          age: '',
          email: '',
        },
        regRules: {
          sex: [{
            required: true,
            message: '性别不能为空',
            trigger: 'change'
          }],
          account: [{
            required: true,
            message: '请输入用户名',
            trigger: 'blur'
          }],
          pwd: [{
            required: true,
            message: '请输入密码',
            trigger: 'blur'
          },{ required: true, trigger: 'blur', validator: validatePassword }],
          name: [{
            required: true,
            message: '请输入名称',
            trigger: 'blur'
          }],
          age: [{ type: 'number', message: '年龄必须为数字值', trigger: 'blur'}]
        },
        loading: false,
        passwordType: 'password',
        redirect: undefined
      }
    },
    methods: {
      showPwd() {
        if (this.passwordType === 'password') {
          this.passwordType = ''
        } else {
          this.passwordType = 'password'
        }
        this.$nextTick(() => {
          this.$refs.password.focus()
        })
      },
      handleReg() {
        this.$refs.regForm.validate(valid => {
        if (valid) {
          this.loading = true
            register(this.regForm).then(response => {
              this.$router.push('/login')
              this.loading = false
              this.$message({
                message: '注册成功',
                type: 'success'
              });
            }).catch(error => {
              this.loading = false
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
      },
      toLog() {
        this.$router.push('/login')
      }
    }
  }
  </script>
  
  <style lang="scss">
  $bg:#283443;
  $light_gray:#fff;
  $cursor: #fff;
  
  @supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
    .login-container .el-input input {
      color: $cursor;
    }
  }
  
  /* reset element-ui css */
  .reg-container {
    .el-input {
      display: inline-block;
      height: 50px;
      width: 100%;
  
      input {
        background: transparent;
        border: 0px;
        -webkit-appearance: none;
        border-radius: 0px;
        padding: 7px 5px 12px 30px;
        color: $light_gray;
        height: 50px;
        caret-color: $cursor;
  
        &:-webkit-autofill {
          box-shadow: 0 0 0px 1000px $bg inset !important;
          -webkit-text-fill-color: $cursor !important;
        }
      }

    }
  
    .el-form-item {
      border: 1px solid rgba(255, 255, 255, 0.1);
      background: rgba(0, 0, 0, 0.1);
      border-radius: 5px;
      color: #454545;
      label {
        padding: 5px 10px 10px 20px;
      }
    }

    .el-radio{
      display: inline-block;
      margin-top: 11px;
    }
  }
  </style>
  
  <style lang="scss" scoped>
  $bg:#2d3a4b;
  $dark_gray:#889aa4;
  $light_gray:#eee;
  
  .reg-container {
    min-height: 100%;
    width: 100%;
    background-color: $bg;
    overflow: hidden;
  
    .reg-form {
      position: relative;
      width: 520px;
      max-width: 100%;
      padding: 50px 35px 0;
      margin: 0 auto;
      overflow: hidden;
    }

    .textarea {
        background: transparent;
        background-color: transparent;
        border: 0px;
        -webkit-appearance: none;
        border-radius: 0px;
        padding: 7px 5px 12px 10px;
        color: $light_gray;
        height: auto;
      }

    .tips {
      font-size: 14px;
      color: #fff;
      margin-bottom: 10px;
  
      span {
        &:first-of-type {
          margin-right: 16px;
        }
      }
    }
  
    .svg-container {
      padding: 6px 5px 6px 15px;
      color: $dark_gray;
      vertical-align: middle;
      width: 30px;
      display: inline-block;
    }
  
    .title-container {
      position: relative;
  
      .title {
        font-size: 26px;
        color: $light_gray;
        margin: 0px auto 40px auto;
        text-align: center;
        font-weight: bold;
      }
    }
  
    .show-pwd {
      position: absolute;
      right: 10px;
      top: 7px;
      font-size: 16px;
      color: $dark_gray;
      cursor: pointer;
      user-select: none;
    }
  }
  </style>
  